package loginbean;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class CreateObject
 */
public class CreateObject extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        ResultSet rs;
        Statement st = null;
        Connection conn = null;

        int tenantId = 0;
        int objectId = 0;
        String objectName = request.getParameter("objectName");
        String fieldName1 = request.getParameter("fieldName1");
        String fieldName2 = request.getParameter("fieldName2");
        String fieldName3 = request.getParameter("fieldName3");
        String fieldName4 = request.getParameter("fieldName4");
        String fieldName5 = request.getParameter("fieldName5");

        String fieldType1 = request.getParameter("fieldType1");
        String fieldType2 = request.getParameter("fieldType2");
        String fieldType3 = request.getParameter("fieldType3");
        String fieldType4 = request.getParameter("fieldType4");
        String fieldType5 = request.getParameter("fieldType5");

        //String tenantEmail = request.getParameter("tenantEmail");
        //String tenantPassword = request.getParameter("tenantPassword");

        try {
            System.out.println("before classforname");
            conn = new DBManager().getDatabaseConnection();
            System.out.println("after classforname connection");

            HttpSession session = request.getSession(true);
            tenantId = Integer.parseInt(session.getAttribute("tenantid").toString());

            //insert into Object table
            String sql = "insert into mydb.objecttable (TenantID,ObjectName)"
                    + " VALUES(?,?)";

            PreparedStatement pst = conn.prepareStatement(sql);
            System.out.println(sql);
            pst.setInt(1, tenantId);
            pst.setString(2, objectName);
            int numRowsChanged = pst.executeUpdate();
            pst.close();

            //to get Object ID :(
            sql = "select ObjectID from mydb.objecttable where"
                    + " TenantID=" + tenantId + " and  ObjectName='" + objectName + "'";
            System.out.println(sql);
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            int count = 0;
            while (rs.next()) {
                objectId = rs.getInt(1);
                count++;
            }
            //insert into Field table - Field 1 dets
            sql = "insert into mydb.fieldtable (ObjectID,FieldName,DataType,IsIndexed,FieldNum)"
                    + " VALUES(?,?,?,?,?)";

            pst = conn.prepareStatement(sql);
            System.out.println("insert field1" + sql);
            pst.setInt(1, objectId);
            pst.setString(2, fieldName1);
            pst.setString(3, fieldType1);
            pst.setBoolean(4, false);
            pst.setInt(5, 1);
            numRowsChanged = pst.executeUpdate();
            pst.close();

            if (fieldName2 != "") {
                //insert into Field table - Field 1 dets
                sql = "insert into mydb.fieldtable (ObjectID,FieldName,DataType,IsIndexed,FieldNum)"
                        + " VALUES(?,?,?,?,?)";

                pst = conn.prepareStatement(sql);
                System.out.println("insert field1" + sql);
                pst.setInt(1, objectId);
                pst.setString(2, fieldName2);
                pst.setString(3, fieldType2);
                pst.setBoolean(4, false);
                pst.setInt(5, 2);
                numRowsChanged = pst.executeUpdate();
                pst.close();

            }
            if (fieldName3 != "") {
                //insert into Field table - Field 1 dets
                sql = "insert into mydb.fieldtable (ObjectID,FieldName,DataType,IsIndexed,FieldNum)"
                        + " VALUES(?,?,?,?,?)";

                pst = conn.prepareStatement(sql);
                System.out.println("insert field1" + sql);
                pst.setInt(1, objectId);
                pst.setString(2, fieldName3);
                pst.setString(3, fieldType3);
                pst.setBoolean(4, false);
                pst.setInt(5, 3);
                numRowsChanged = pst.executeUpdate();
                pst.close();

            }

            if (fieldName4 != "") {
                //insert into Field table - Field 1 dets
                sql = "insert into mydb.fieldtable (ObjectID,FieldName,DataType,IsIndexed,FieldNum)"
                        + " VALUES(?,?,?,?,?)";

                pst = conn.prepareStatement(sql);
                System.out.println("insert field1" + sql);
                pst.setInt(1, objectId);
                pst.setString(2, fieldName4);
                pst.setString(3, fieldType4);
                pst.setBoolean(4, false);
                pst.setInt(5, 4);
                numRowsChanged = pst.executeUpdate();
                pst.close();

            }
            if (fieldName5 != "") {
                //insert into Field table - Field 1 dets
                sql = "insert into mydb.fieldtable (ObjectID,FieldName,DataType,IsIndexed,FieldNum)"
                        + " VALUES(?,?,?,?,?)";

                pst = conn.prepareStatement(sql);
                System.out.println("insert field1" + sql);
                pst.setInt(1, objectId);
                pst.setString(2, fieldName5);
                pst.setString(3, fieldType5);
                pst.setBoolean(4, false);
                pst.setInt(5, 5);
                numRowsChanged = pst.executeUpdate();
                pst.close();

            }
            if (numRowsChanged > 0) {
                //session.setAttribute("username",);	
                response.sendRedirect("welcome.jsp");
            } else {
                response.sendRedirect("error.jsp");
            }

        } catch (ClassNotFoundException e) {
            System.out.println("Couldn't load database driver: " + e.getMessage());
        } catch (SQLException e) {
            System.out.println("SQLException caught: " + e.getMessage());
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }

    }
}
